﻿package Framework.Protocol.CDFProtocol
{
    import Framework.Util.*;
    import flash.utils.*;

    public class Protocol extends Object implements IProtocol
    {
        private var _bytesBuffer:ByteArray;
        private var _readHead:Boolean;
        private var _protocolHead:ProtocolHead;
        private static const templeBytes:ByteArray = initTempleBytes();

        public function Protocol()
        {
            this._readHead = false;
            this._protocolHead = new ProtocolHead();
            this._bytesBuffer = new ByteArray();
            this._bytesBuffer.endian = Endian.LITTLE_ENDIAN;
            return;
        }// end function

        public function handleData(param1:ByteArray, param2:IBusinessHandler, param3:IIOHandler, param4:IBusinessHandler) : int
        {
            var _loc_5:ByteArray = null;
            var _loc_6:Boolean = false;
            var _loc_7:ByteArray = null;
            this._bytesBuffer.position = this._bytesBuffer.length;
            this._bytesBuffer.writeBytes(param1, 0, param1.length);
            this._bytesBuffer.position = 0;
            while (true)
            {
                
                if (this._readHead)
                {
                    if (this._bytesBuffer.length - this._bytesBuffer.position >= this._protocolHead.messageSize)
                    {
                        _loc_5 = new ByteArray();
                        _loc_5.endian = Endian.LITTLE_ENDIAN;
                        this._bytesBuffer.readBytes(_loc_5, 0, this._protocolHead.messageSize);
                        this._readHead = false;
                        if (this._protocolHead.compressionStatus == 1)
                        {
                            _loc_5.uncompress();
                        }
                        _loc_6 = param2.handlePacket(_loc_5, param4);
                        _loc_5.clear();
                        if (!_loc_6)
                        {
                            return -1;
                        }
                    }
                    else
                    {
                        break;
                    }
                    continue;
                }
                if (this._bytesBuffer.length - this._bytesBuffer.position < ProtocolHead.CDF_PROTOCOL_HEAD_SIZE)
                {
                    break;
                }
                this._protocolHead.__read(this._bytesBuffer);
                this._readHead = true;
                if (this._protocolHead.messageSize > ProtocolHead.CDF_PROTOCOL_MAX_SIZE || this._protocolHead.messageSize < 0)
                {
                    return -1;
                }
            }
            if (this._bytesBuffer.position != 0)
            {
                _loc_7 = new ByteArray();
                _loc_7.writeBytes(this._bytesBuffer, this._bytesBuffer.position);
                this._bytesBuffer.clear();
                this._bytesBuffer = _loc_7;
                this._bytesBuffer.endian = Endian.LITTLE_ENDIAN;
            }
            return param1.length;
        }// end function

        public function sendDataEncrypt(param1:ByteArray, param2:IIOHandler) : Boolean
        {
            if (param1.length > ProtocolHead.CDF_PROTOCOL_MAX_SIZE)
            {
                return false;
            }
            templeBytes.position = 0;
            var _loc_3:* = new ProtocolHead();
            _loc_3.messageSize = param1.length;
            _loc_3.__write(templeBytes);
            templeBytes.writeBytes(param1, 0, param1.length);
            return param2.sendData(templeBytes, templeBytes.position) != -1;
        }// end function

        public function clear() : void
        {
            this._bytesBuffer.clear();
            this._readHead = false;
            return;
        }// end function

        private static function initTempleBytes() : ByteArray
        {
            var _loc_1:* = new ByteArray();
            _loc_1.endian = Endian.LITTLE_ENDIAN;
            return _loc_1;
        }// end function

    }
}
